package kotlinusage

import org.junit.Test

//使用typealias别名定义lambda表达式
typealias NumPrint = (Int) -> Unit

class TestInlineClass {

    //JvmField只能用来修饰非private字段,不再生成setter/getter方法.
    @JvmField
    val hobby: String = "游泳"

    @JvmField
    var grade: String = "年级"

    @Test
    fun testInlineClass() {
        val person = Person("HaHa")
        println("value:${person.getName()}")
    }

    //JvmOverloads关键字:对于每一个有默认值的参数，生成的重载会把当前有默认值的参数及其右边的参数都去掉，所以如果方法中所有的参数都有默认值，生成的重载函数中还会有一个无参的重载函数。
    @JvmOverloads
    fun method(value: Int, filed: String = "a", backGround: String = "back") {

    }

    @Test
    fun testForLoopWithTwoParams() {
        for ((i, j) in (0.until(120)).zip(0..100)) {
            println("i-${i},j-${j}")
        }
    }

    @Test
    fun testForLoopWithThreeParams() {
        for ((pair, j) in (0..20).zip(30..40).zip(50..80)) {
            println("i-first:${pair.first},i-second:${pair.second},j-${j}")
        }
    }

    @Test
    fun testLambdaTypeAlias() {
        oddNumber.invoke(11)
    }

    private val oddNumber : NumPrint = {
        if (it % 2 == 0) {
            println("it--${it}")
        } else {
            println("This is even number.")
        }
    }

    @Test
    fun testMemberReference() {
        //成员引用类型和lambda一起使用
        val students = listOf(
            Student(name = "Alice", age = 18),
            Student(name = "Mikyou", age = 20),
            Student(name = "Bob", age = 16)
        )
        println(students.maxBy(Student::age))

        println("timeValue:${timeValue}")
    }

    private val timeValue : Long
        get() {
            return System.currentTimeMillis()
        }
}